package com.zhaoxi.project.sys.service;

import com.zhaoxi.framework.web.PageData;
import com.zhaoxi.framework.web.BaseService;
import com.zhaoxi.project.sys.mapper.Example3Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class Example3Service extends BaseService {

  @Autowired
  private Example3Mapper exampleMapper;

  //查询数据集合
  public PageData getList(PageData pd) {

    List<PageData> list = exampleMapper.getList(pd);
    return this.responses(list);
    
  }

  //查询数据对象 通过id
  public PageData getById(PageData pd) {
    PageData pd1 = new PageData();
    if (pd.getLong("id") == null){
      return null;
    }
    pd1.put("id",pd.getLong("id"));
    List<PageData> list = exampleMapper.getList(pd1);
    if (list.size() > 0){
      return list.get(0);
    }
    return null;
  }
  //查询数据行数
  public PageData getCount(PageData pd) {

    return this.success(exampleMapper.getCount(pd));
    
  }

  //新增
  public PageData add(PageData pd) {

//    获取返回的主键id
    int row = exampleMapper.add(pd);
    if (row == 0) {
      return this.responses(row);
      
    }
    List<PageData> list = pd.getListPd("line");
    int i = 0;
    while (list.size() > i) {
      PageData pd1 = list.get(i);
      pd1.put("id", pd.getLong("id"));
      this.addLine(pd1);
      i++;
    }
    return this.responses(row);
    
  }

  //编辑，同时如果有子单据，也要处理
  public PageData edit(PageData pd) {

    pd.put("updateBy", this.getUsername());
    int row = exampleMapper.edit(pd);
    if (row == 0) {
      return this.responses(row);
    }
    Long id = pd.getLong("id");
    List<PageData> list = pd.getListPd("line");
    for (PageData pd1 : list) {
      if (pd1.getLong("id") == null) {
        pd1.put("id", id);
        this.addLine(pd1);
      } else {
        this.editLine(pd1);
      }
    }
    return this.responses(row);
    
  }

  //删除
  public PageData del(Long id) {

    if (id == null){
      return this.error("删除单据id不能为空");
      
    }
    PageData pd = new PageData();
    pd.put("id",id);
    exampleMapper.delLine(pd);
    return this.responses(exampleMapper.del(id));
    
  }


  //子表数据的增删改查================================
  //查询数据集合
  public List<PageData> getLineList(PageData pd) {
    return exampleMapper.getLineList(pd);
  }

  //查询数据行数
  public PageData getLineCount(PageData pd) {

    return this.success(exampleMapper.getLineCount(pd));
    
  }

  //新增
  public PageData addLine(PageData pd) {

    return this.responses(exampleMapper.addLine(pd));
    
  }

  //删除
  public PageData editLine(PageData pd) {

    return this.responses(exampleMapper.editLine(pd));
    
  }

  //删除
  public PageData delLine(PageData pd) {

    return this.responses(exampleMapper.delLine(pd));
    
  }

//  =================tree关联表的数据处理

  /**
   * 查询数据集合
   */
  public List<Long> getTreeList(PageData pd) {
    return  exampleMapper.getTreeList(pd);
  }

  //查询数据行数
  public PageData getTreeCount(PageData pd) {

    return this.success(exampleMapper.getTreeCount(pd));
    
  }

  //新增
  public PageData addTree(PageData pd) {

    return this.responses(exampleMapper.addTree(pd));
    
  }

  /**
   * 修改关联表数据集合
   */
  @Transactional
  public PageData editTree(PageData pd) {

//    删除原有的关联
    Long id = pd.getLong("id");
    this.delTree(id);

//    新增新选的
    List<Long> treeChecked = (List<Long>) pd.get("treeChecked");

    int i = 0;
    while (treeChecked.size() > i) {
      PageData tree = new PageData();
      tree.put("id", id);
      tree.put("treeId", treeChecked.get(i));
      exampleMapper.addTree(tree);
      i++;
    }
    return this.responses(i);
    
  }

  //删除
  public PageData delTree(Long id) {

    return this.responses(exampleMapper.delTree(id));
    
  }
}
